﻿using InnoWeb.CEND.BL.Service;
using InnoWeb.CEND.Core.Model.User;
using InnoWeb.CEND.Data;
using InnoWeb.CEND.Data.Infrastructure;
using InnoWeb.CEND.UI.MVC.Areas.Admin.Filters;
using InnoWeb.CEND.UI.MVC.Areas.Admin.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using WebMatrix.WebData;

namespace InnoWeb.CEND.UI.MVC.Areas.Admin.Controllers
{
    [Authorize]
    public class AccountController : Controller
    {
        private static WebSiteContext _context;
        private static IUnitOfWork _unitOfWork;

        private static GenericRepository<User> _userRepository;
        private static UserService _userService;

        public AccountController()
        {
            _context = new WebSiteContext(ConfigurationManager.ConnectionStrings["CendDBConnection"].ConnectionString);
            _unitOfWork = new UnitOfWork(_context);

            _userRepository = new GenericRepository<User>(_context);

            _userService = new UserService(_unitOfWork, _userRepository);
        }

        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(AccountModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                string uName = model.User.UserName;
                string pass = model.User.Password;

                bool isValid = _userService.GetAllElements().Any(a => a.UserName == uName && a.Password == pass);
                if (isValid)
                {
                    FormsAuthentication.SetAuthCookie(uName,model.RememberMe);

                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
            //if (ModelState.IsValid && WebSecurity.Login(model.User.UserName, model.User.Password, persistCookie: model.RememberMe))
            //{
            //    return RedirectToLocal(returnUrl);
            //}

            // If we got this far, something failed, redisplay form
            //ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }

        //public ActionResult LogOff()
        //{
        //    return View();
        //}


        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult LogOff()
        {
            //WebSecurity.Logout();
            FormsAuthentication.SignOut();

            return RedirectToAction("Login", "Account");
        }

        private ActionResult RedirectToLocal(string returnUrl)
        {
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home", RouteData.Values["admn_dflt"]);
            }
        }

        private bool LoginMethod(string userName, string pass, bool remember)
        {
            //return WebSecurity.Login(userName, pass, persistCookie: remember);

            if (userName == "cend" && pass == "sanane")
                return true;
            else
                return false;
        }

    }
}
